package darknet.application.chat.impl.net;

import darknet.core.spec.net.Protocol;

/**
 * Protocol for Chat system.
 * 
 * @author Yoyo
 */
@SuppressWarnings("serial")
public class ChatProtocol implements Protocol {
    
    /** Instance of the last version of ChatProtocol. */
    private static ChatProtocol CURRENT;
    
    /**
     * Get the current version of Chat Protocol.
     * @return
     */
    public synchronized static ChatProtocol getCurrent() {
	
	if(CURRENT == null) {
	    
	    String name = "Chat Protocol";
	    String version = "0.1";
	    CURRENT =  new ChatProtocol(name, version);
	}
	return CURRENT;
    }
    
    /** Protocol's name */
    private String name;
    
    /** Protocol's version */
    private String version;
    
    /**
     * Initialise a new Chat protocol.
     * 
     * @param name
     * @param version
     */
    private ChatProtocol(String name, String version) {
	
	if (name == null || name.length() == 0) {
	    throw new IllegalArgumentException("name is invalid");
	}
	this.name = name;
	
	if (version == null || version.length() == 0) {
	    throw new IllegalArgumentException("version is invalid");
	}
	this.version = version;
    }

    @Override
    public String getName() {
	return name;
    }

    @Override
    public String getVersion() {
	return version;
    }

    @Override
    public boolean equals(Object obj) {
	return obj instanceof ChatProtocol && 
		((ChatProtocol)obj).name.equals(name) &&
		((ChatProtocol)obj).version.equals(version);
    }

    @Override
    public int hashCode() {
	return toString().hashCode();
    }

    @Override
    public String toString() {
	return new StringBuilder().append("name: ").append(name)
	.append("; version:").append(version).toString();
    }
}
